<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ page import="java.util.Date"%>
<% String formId = "form_" + new Date().getTime();
   String gridId = "grid_" + new Date().getTime();
   String path = request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);
%>
<script type="text/javascript">

$(function (){
	var grid = $('[data-role="hospGrid"]');
    var form = $("[name='hospForm']");
	PageLoader = {
	   //
	    initSearchPanel:function(){},
	    initGridPanel: function(){
	         var self = this;
	         var width = 152;
	         return grid.grid({
	                identity:"hospCode",
	                buttons: [
	                        {content: '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-plus"><span>添加</button>', action: 'add'},
	                        {content: '<button class="btn btn-success" type="button"><span class="glyphicon glyphicon-edit"><span>修改</button>', action: 'modify'},
	                        {content: '<button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button>', action: 'delete'}
	                    ],
	                url:"${pageContext.request.contextPath}/HospInfo/pageJson.dhc",
	                columns: 
                	[
					 { title: '显示顺序', name: 'hospOrder', width: '20%'},
                	 { title: '院区代码', name: 'hospCode', width: '20%'},
                	 { title: '院区名称', name: 'hospName', width: '20%'},
     	             { title: '院区简称', name: 'hospShortName', width: '20%'},
     	             { title: '院区级别', name: 'hospLevel', width: '20%'}
	                ]
	         }).on({
	                   'add': function(){
	                       self.add($(this));
	                   },
	                   'modify': function(event, data){
	                        var indexs = data.data;
	                        var $this = $(this);
	                        if(indexs.length == 0){
	                            drgUI.warning('请选择一条记录进行修改');
	                            return;
	                        }
	                        if(indexs.length > 1){
	                             drgUI.warning('只能选择一条记录进行修改');
	                            return;
	                        }
	                       self.modify(data.item[0],indexs[0], $this);
	                    },
	                   'delete': function(event, data){
	                        var indexs = data.data;
	                        var $this = $(this);
	                        if(indexs.length == 0){
	                            drgUI.warning('请选择要删除的记录！');
	                            return;
	                        }
	                        var remove = function(){
	                            self.remove(indexs, $this);
	                        };
	                        drgUI.confirm({
	                            content: '确定要删除所选记录吗?',
	                            callSure: remove
	                        });
	                   }
	         });
	    },
	    add: function(grid){
	        var self = this;
	        var dialog = $('<div class="modal fade"><div class="modal-dialog">'
	        	+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" '
	        	+'data-dismiss="modal" aria-hidden="true">&times;</button>'
	        	+'<h4 class="modal-title">新增</h4></div><div class="modal-body">'
	        	+'<p>One fine body&hellip;</p></div><div class="modal-footer">'
	        	+'<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
	        	+'<button type="button" class="btn btn-success" id="save">保存</button></div></div>'
	        	+'</div></div>');
	        $.get('<%=path%>/hosp-add.jsp').done(function(html){
	            dialog.modal({
	                keyboard:false
	            }).on({
	                'hidden.bs.modal': function(){
	                    $(this).remove();
	                }
	            }).find('.modal-body').html(html);
	            self.initPage(dialog.find('form'));
	        });
	        dialog.find('#save').on('click',{grid: grid}, function(e){
	              if(!Validator.Validate(dialog.find('form')[0],3))return;
	              var hospCode = dialog.find('#hospCodeID').val();
	              if(hospCode.length<8){
	            	  drgUI.warning('院区编码请设置为8位以上！');
	              }else{
	            	  $.post('${pageContext.request.contextPath}/HospInfo/isHospOrderExist.dhc',{'hospOrder':dialog.find('#hospOrderID').val()}).done(function(result){
	            		  if(result) {
	                			drgUI.error('显示顺序已存在，请重新输入!');
	                		} else {
	                			 $.post('${pageContext.request.contextPath}/HospInfo/add.dhc', dialog.find('form').serialize()).done(function(result){
	      	  	                   if(result.success ){
	      	  	                        dialog.modal('hide');
	      	  	                        e.data.grid.data('koala.grid').refresh();
	      	  	                        drgUI.success('保存成功！');
	      	  	                    }else{
	      	  	                       drgUI.error('保存失败,'+result.errorMessage);
	      	  	                     }
	      	  	              	});
	                		}
	            	  });
	              }
	        });
	    },
	    modify: function(item,id, grid){
	        var self = this;
	        var dialog = $('<div class="modal fade"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">修改</h4></div><div class="modal-body"><p>One fine body&hellip;</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button><button type="button" class="btn btn-success" id="save">保存</button></div></div></div></div>');
	        $.get('<%=path%>/hosp-update.jsp').done(function(html){
	               dialog.find('.modal-body').html(html);
	               self.initPage(dialog.find('form'));
	               dialog.find("#hospOrderIDUpdate").val(item.hospOrder);
	               dialog.find("#hospNameIDUpdate").val(item.hospName);
	               dialog.find("#hospShortNameIDUpdate").val(item.hospShortName);
	               dialog.find("#hospLevelIDUpdate").val(item.hospLevel);
	               
	                dialog.modal({
	                    keyboard:false
	                }).on({
	                    'hidden.bs.modal': function(){
	                        $(this).remove();
	                    }
	                });
	                dialog.find('#save').on('click',{grid: grid}, function(e){
	                    if(!Validator.Validate(dialog.find('form')[0],3))return;
	                    var hospCode = item.hospCode;
	                    $.post('${pageContext.request.contextPath}/HospInfo/isHospOrderExist.dhc',{'hospOrder':dialog.find('#hospOrderIDUpdate').val(),'hospCode':hospCode}).done(function(result){
		            		  if(result) {
		                			drgUI.error('显示顺序已存在，请重新输入!');
		                		} else {
		                			 $.post('${pageContext.request.contextPath}/HospInfo/update.dhc?hospCode='+hospCode, dialog.find('form').serialize()).done(function(result){
		     	                        if(result.success){
		     	                            dialog.modal('hide');
		     	                            e.data.grid.data('koala.grid').refresh();
		     	                            drgUI.success('保存成功！');
		     	                        }else{
		     	                            drgUI.error(result.actionError);
		     	                        }
		     	                    });
		                		}
	                    });
	                });
	        });
	    },
	    initPage: function(form){
	              form.find('.form_datetime').datetimepicker({
	                   language: 'zh-CN',
	                   format: "yyyy-mm-dd",
	                   autoclose: true,
	                   todayBtn: true,
	                   minView: 2,
	                   pickerPosition: 'bottom-left'
	               }).datetimepicker('setDate', new Date());//加载日期选择器
	               form.find('.select').each(function(){
	                    var select = $(this);
	                    var idAttr = select.attr('id');
	                    select.select({
	                        title: '请选择',
	                        contents: selectItems[idAttr]
	                    }).on('change', function(){
	                        form.find('#'+ idAttr + '_').val($(this).getValue());
	                    });
	               });
	    },
	    remove: function(ids, grid){
	    	var data = [{ name: 'hospCodes', value: ids.join(',') }];
	    	$.post('${pageContext.request.contextPath}/HospInfo/delete.dhc', data).done(function(result){
	                        if(result.success){
	                            grid.data('koala.grid').refresh();
	                            drgUI.success('删除成功');
	                        }else{
	                            drgUI.error('error');
	                        }
	    	});
	    }
	}
	PageLoader.initSearchPanel();
	PageLoader.initGridPanel();
	form.find('#search').on('click', function(){
            if(!Validator.Validate(document.getElementById("<%=formId%>"),3))return;
            var params = {};
            form.find('input').each(function(){
                var $this = $(this);
                var name = $this.attr('name');
                if(name){
                    params[name] = $this.val();
                }
            });
            grid.getGrid().search(params);
        });
});
</script>
</head>
<body>
<div style="width:98%;margin-right:auto; margin-left:auto; padding-top: 15px;">
<!-- search form -->
<%-- <form name="hospForm" id=<%=formId%> target="_self" class="form-horizontal">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    	<div class="form-group">
    		<label class="control-label" style="width:100px;float:left;">院区编码:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
            	<input name="hospCode" class="form-control" type="text" style="width:160px;" id="hospCodeID"  />
			</div>
    		<label class="control-label" style="width:100px;float:left;">院区名称:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
            	<input name="hospName" class="form-control" type="text" style="width:160px;" id="hospNameID"  />
			</div>
            <label class="control-label" style="width:100px;float:left;">院区简称:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
            	<input name="hospShortName" class="form-control" type="text" style="width:160px;" id="hospShortNameID"  />
        	</div>
        	<label class="control-label" style="width:100px;float:left;">院区级别:&nbsp;</label>
            <div style="margin-left:15px;float:left;">
	            <input name="hospLevel" class="form-control" type="text" style="width:160px;" id="hospLevelID"  />
	        </div>
		</div>
	</td>
	<td style="vertical-align: bottom;"><button id="search" type="button" style="position:relative; margin-left:35px; top: -15px" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>&nbsp;查询</button></td>
  </tr>
</table>	
</form>
 --%>
<form name="hospForm" id=<%=formId%> target="_self" class="form-horizontal">
<div class="container-fluid">
		<div class="row">
			<div class="form-group col-md-3">
	    		<label for="hospCode">院区编码:&nbsp;</label>
	            <input name="hospCode" class="form-control" type="text" style="width:120px;" id="hospCodeID"  />
			</div>
			<div class="form-group col-md-3">
				<label for="hospName">院区名称:&nbsp;</label>
	            <input name="hospName" class="form-control" type="text" style="width:120px;" id="hospNameID"  />
			</div>
			<div class="form-group col-md-3">
				<label for="hospShortName">院区简称:&nbsp;</label>
	            <input name="hospShortName" class="form-control" type="text" style="width:120px;" id="hospShortNameID"  />
			</div>
			<div class="form-group col-md-3">
				<label for="hospLevel">院区级别:&nbsp;</label>
	            <input name="hospLevel" class="form-control" type="text" style="width:120px;" id="hospLevelID"  />
			</div>
			<div class="form-group col-md-1">
				<button id="search" type="button" style="margin-left:5px;" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>&nbsp;查询</button>
			</div>
	   </div>
	
</div>
</form>
<!-- grid -->
<div data-role="hospGrid" id=<%=gridId%>></div>
</div>
</body>
</html>
